package com.everflourish.yeah100.act.statistics;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListPopupWindow;
import android.widget.TextView;

import com.everflourish.yeah100.R;
import com.everflourish.yeah100.act.base.BaseActivity;
import com.everflourish.yeah100.adapter.ClassRanksAdapter;
import com.everflourish.yeah100.adapter.StatisticTeacherAdapter;
import com.everflourish.yeah100.entity.Examination;
import com.everflourish.yeah100.entity.statistics.ClassRanks;
import com.everflourish.yeah100.entity.statistics.ClassRanksModel;
import com.everflourish.yeah100.utils.IntentUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Created by liqisi on 2016/7/6.
 * 时间：2016/7/6
 */
public class TeacherStatisticsActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {

    private DrawerLayout mDrawerLayout;
    private Toolbar mToolBar;
    private TabLayout mTabLayout;
    public ViewPager mViewPager;
    private FragmentManager mFragmentManager;
    private List<Fragment> mFragments;
    private FragmentTeacherGrade mFragmentTeacherGrade;
    private FragmentTeacherError mFragmentTeacherError;
    private FragmentTeacherClass mFragmentTeacherClass;
    private StatisticTeacherAdapter mStatisticTeacherAdapter;
    private String[] mTitles;
    public Examination mExamination;

    private LinearLayout mGradeFilterLl;
    public TextView mClassRanksEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statistics_teacher);
        initData();
        initFragment();
        initView();
        configView();
    }

    private void initData() {
        mExamination = (Examination) getIntent().getSerializableExtra(
                IntentUtil.EXAMINATION);
        mFragmentManager = getSupportFragmentManager();
        mTitles = getResources().getStringArray(R.array.statistics_teacher_titles);
    }

    private void initFragment() {
        mFragments = new ArrayList<Fragment>();
        mFragmentTeacherGrade = new FragmentTeacherGrade();
        mFragmentTeacherError = new FragmentTeacherError();
        mFragmentTeacherClass = new FragmentTeacherClass();
        mFragments.add(mFragmentTeacherGrade);
        mFragments.add(mFragmentTeacherError);
        mFragments.add(mFragmentTeacherClass);
    }

    private void initView() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.statistics_teacher_drawer_ly);
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        mGradeFilterLl = (LinearLayout) findViewById(R.id.statistics_teacher_grade_filter);
        mClassRanksEt = (TextView) findViewById(R.id.teacher_statistics_class_rank_et);

        mToolBar = (Toolbar) findViewById(R.id.toolbar_statistics_teacher);
        mTabLayout = (TabLayout) findViewById(R.id.tab_statistics_teacher);
        mViewPager = (ViewPager) findViewById(R.id.viewpager_statistics_teacher);
        mStatisticTeacherAdapter = new StatisticTeacherAdapter(getSupportFragmentManager(), mTitles, mFragments);
        mViewPager.setAdapter(mStatisticTeacherAdapter);
        // 在setAdapter之后调用
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.addOnPageChangeListener(this);
        mTabLayout.setupWithViewPager(mViewPager);
//        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTabLayout.setTabsFromPagerAdapter(mStatisticTeacherAdapter);
    }

    private void configView() {
        mToolBar.setTitle("成绩表");
        setSupportActionBar(mToolBar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishMenuActivity();
            }
        });
        mClassRanksEt.setOnClickListener(this);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finishMenuActivity();
            return true;
        } else {
            return super.onKeyUp(keyCode, event);
        }
    }

    private void finishMenuActivity() {
        IntentUtil.finishActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mQueue != null) {
            mQueue.stop();
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mToolBar.setTitle(mTitles[position]);
        switch (position) {
            case 0:
                mGradeFilterLl.setVisibility(View.VISIBLE);
                break;
            case 1:
                mGradeFilterLl.setVisibility(View.GONE);
                break;
            case 2:
                mGradeFilterLl.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.teacher_statistics_class_rank_et:
                showDialogItems();
                break;
        }
    }

    /**
     * 选择科目lsit弹出框
     */
    private void showDialogItems() {
        final List<ClassRanksModel> classRanksModelList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            ClassRanksModel classRanksModel = new ClassRanksModel();
            ClassRanks classRanks = new ClassRanks();
            classRanksModel.setClassRanks(classRanks);
            classRanksModel.setIsChecked(false);
            classRanksModelList.add(classRanksModel);
        }
        ListPopupWindow mSubjectPopupWindow = new ListPopupWindow(this);
        final ClassRanksAdapter classRanksAdapter = new ClassRanksAdapter(mContext, this, classRanksModelList);
        mSubjectPopupWindow.setAdapter(classRanksAdapter);
        mSubjectPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mSubjectPopupWindow.setHeight(600);
        mSubjectPopupWindow.setAnchorView(mClassRanksEt);//设置ListPopupWindow的锚点，即关联PopupWindow的显示位置和这个锚点
        mSubjectPopupWindow.setModal(true);// 设置是否是模式
        mSubjectPopupWindow.show();
    }
}